import 'package:flutter/material.dart';

class Mine extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('个人中心'),
      ),
      body: new MineWidget()
    );
  }
}

class MineWidget extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return new MineState();
  }
}

class MineState extends State<MineWidget> with TickerProviderStateMixin{
  final _messages = <ChatMessage>[];
  final TextEditingController _textEditingController = new TextEditingController();
  bool _isCompleted = false;
  @override
  Widget build(BuildContext context) {
      return new Column(
        children: <Widget>[
          _messagesList(),
          new Divider(),
          _inputForm(),
        ],
      );
  }
  @override
  void dispose() {                                                   //new
    for (ChatMessage message in _messages)                           //new  遍历_messages数组
      message.animationController.dispose();                         //new  释放动效
    super.dispose();                                                 //new
  } 

  Widget _messagesList() {
    return new Flexible(
      child: new ListView.builder(
        itemBuilder: (_, i) {
          return _messages[i];
        },
        itemCount: _messages.length,
      ),
    );
  }

  Widget _inputForm() {
    return new Container(
      padding: EdgeInsets.fromLTRB(20.0, 0, 20.0, 0),
      child: new Row(
        children: <Widget>[
          new Flexible(
            child: new TextField(
              controller: _textEditingController,
              onSubmitted: _handleSubmited,
              onChanged: (String text) {
                setState(() {
                  _isCompleted = text.length > 0;
                });
              },
              decoration: new InputDecoration.collapsed(hintText:'输入信息'),
            )
          ),
          new IconButton(
            icon: new Icon(Icons.send),
            onPressed: _isCompleted ? () => _handleSubmited(_textEditingController.text) : null,
          )
        ],
      ),
    );
  }

  _handleSubmited(String text) {
    _textEditingController.clear();
    ChatMessage chatMessage = new ChatMessage(
      msg: text,
      animationController: new AnimationController(
        duration: new Duration(milliseconds: 300),
        vsync: this
      ),
    );
    setState(() {
      _isCompleted = false;
      _messages.add(chatMessage);   
    });
    chatMessage.animationController.forward();
  }
}

class ChatMessage extends StatelessWidget {
  ChatMessage({this.msg, this.animationController});
  final String msg;
  final AnimationController animationController;
  @override
  Widget build(BuildContext context) {
    return new SizeTransition(
      sizeFactor: new CurvedAnimation(
        parent: animationController,
        curve: Curves.easeOut
      ),
      child: new Container(
        margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),
        child: new Row(
          children: <Widget>[
            new CircleAvatar(
              child: new Text('L'),
            ),
            new Expanded(// Expanded 包裹避免文字溢出时报错
              child: new Container(
                margin: EdgeInsets.only(left: 20.0),
                child: new Text(msg),
              ),
            ),
            
          ],
        ),
      ),
    );
  }
}
